*TITLE:INCUMBENT TENURE CROWDS OUT ECONOMIC VOTING
*JOURNAL: BRITISH JOURNAL OF POLITICAL SCIENCE
*AUTHOR: MARTIN VINÆS LARSEN
*FILE: SUBNATIONAL ANALYSES
*DEPENDENCIES: eststo, lmoments, blindschemes, combomarginsplot

*loading dataset*
use "subnational", clear


//TABLE 3//

*running models
eststo a: mixed delta c.unem#c.inc_dif inc_dif borg05 i.muni unem if amal==1, vce(cluster muni)
eststo b: mixed delta c.unem#c.inc_dif turnout logvotes inc_dif borg05 i.muni unem if amal==1, vce(cluster muni)
eststo c: mixed delta c.unem#c.inc_dif mayor bluevotes turnout logvotes inc_dif borg05  i.muni unem if amal==1, vce(cluster muni)

*table
la var delta  "%noshow"
esttab a b c using natexp.tex, replace keep(c.unem#c.inc_dif mayor bluevotes turnout logvotes inc_dif borg05) star(+ 0.10 * 0.05) b(%9.2fc) ///
order(inc_dif unem c.unem#c.inc_dif borg05 mayor bluevotes turnout logvotes) ///
nomtitles title("Linear Regression of Change in Support for the Incumbent Mayoral Party} \footnotesize \label{table:natexp") ///
stats(N, label("Observations") fmt(%9.0fc) ) ///
indicate("\hline Municipality  FE=791.muni", labels("$\checkmark$" " ")) ///
label se nonotes addnotes("Standard errors clustered by municipality in parentheses.") nogaps 

//FIGURE 4//
*restoring estimates
estimates restore a
*drawing figure
margins, dydx(inc_dif) at(unem=(0.9(0.1)3.8)) noestimcheck
marginsplot, level(95) addplot(histogram unem if e(sample)==1, below discret percent lcolor(black*0.5) fcolor(white) yaxis(2) ///
ylabel(0 60, axis(2)) yscale(off axis(2)) xlabel( 1 2 3 4, labsize(medlarge))  ///
ylabel(-10 -5 0 5 , axis(1) labsize(medlarge))) scheme(plotplain) recastci(rline) recast(line)  ///
ytitle( "Precincts with Less ctr. More Experience:" "Change in Electoral Support for Incumbent", ///
size(medlarge)) xtitle(" " "Increase in Unemployment", size(medlarge)) legend(off) ///
plotopts(lwidth(thick)) title(" ") yline(0) 
graph export natexp.eps, replace

///APPENDIX///

*Descriptive statistics (S3)
preserve
drop valgsted muni trich  _est* borg05 borg09 votes
la var delta "Change in Support for Mayoral Party"
file open anyname using munides.txt, write text replace
order delta inc_dif a07 a09 unem turnout  mayor logvotes
file write anyname  _newline  _col(0)  "\begin{table}[htbp] \footnotesize \centering \caption{Descriptive Statistics, Subnational Data} \label{desmuni} \begin{tabular}{l*{10}{c}}\hline\hline"
file write anyname _newline _col(0) "&Mean & SD & Min& Max& n \\  \hline "
foreach  x of varlist * {
su `x', d
file write anyname _newline (`"`: var label `x''"') "  &" _tab %9.2f  (r(mean)) " &" _tab %9.2f (r(sd)) " &" _tab %9.2f  (r(min)) " &" _tab %9.2f  (r(max)) " &" _tab %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname
restore


*Sensitivity to Outliers (S6)
tostring muni, replace
encode muni if amal==1, gen(group)
destring muni, replace

tempfile temp1
postfile results id m1 m2 m3 using `temp1'
forvalues i=0/66{
qui{
mixed delta c.unem#c.inc_dif inc_dif borg05 i.muni unem if amal==1 & group!=`i', vce(cluster muni)
local b1=_b[c.unem#c.inc_dif]
mixed delta c.unem#c.inc_dif turnout logvotes inc_dif borg05 i.muni unem if amal==1 & group!=`i', vce(cluster muni)
local b2=_b[c.unem#c.inc_dif]
mixed delta c.unem#c.inc_dif mayor bluevotes turnout logvotes inc_dif borg05  i.muni unem if amal==1 & group!=`i', vce(cluster muni)
local b3=_b[c.unem#c.inc_dif]
post results (`i') (`b1') (`b2') (`b3')
}
di `i'
}
postclose results
preserve
use `temp1', clear
reshape long m, j(modelnr) i (id) 
gen pipe="|"
twoway scatter modelnr m if id!=0, msym(Oh) mcolor(black*0.7) msize(medlarge) ///
|| scatter modelnr m if id==0, scheme(plotplain) msym(none) mlab(pipe) mlabsize(vlarge) msize(large) ///
 xlabel(-4(0.5)0.0, labsize(medlarge)) ///
 ylabel(0.75 " " 1 "Model 1  " 2 "Model 2 - Demographic Controls " ///
 3 "Model 3 - Political Controls " 3.25 " ", labsize(medlarge) notick angle(0)) legend(off) ///
 xtitle(" " "Estimated interaction (Subnational)", size(medlarge)) ytitle(" ")
 
graph export apd_natexp_outlier.eps, replace
restore



**Interactions (S7)
lmoments unem
tabstat unem, stats(p50) by(trich)
gen unem2=unem-1.7 if trich==0
replace unem2=unem-2.3 if trich==1
replace unem2=unem-2.9 if trich==2

tempfile temp1 temp2

mixed delta i.trich#(c.inc_dif#c.unem2 c.inc_dif) mayor bluevotes turnout logvotes inc_dif borg05  i.muni unem if amal==1, vce(cluster muni)
margins, dydx(inc_dif) at(trich=(0 1 2)) saving(`temp1', replace) level(95) noestimcheck

mixed delta c.unem#c.inc_dif mayor bluevotes turnout logvotes inc_dif borg05  i.muni unem if amal==1, vce(cluster muni)
margins, dydx(inc_dif) at(unem=(0.8(0.2)3.8)) saving(`temp2', replace) level(95) noestimcheck

preserve
use `temp1', clear
append using `temp2'
gen id=0
replace id=1 if _n >3
recode _at1 (0=1.7) (1=2.3) (2=2.9) if id==0
twoway line  _margin _at1 if id==1, lwidth(medthick) || ///
scatter _margin _at1 if id==0, msym(O) msize(medlarge) mcolor(black) || ///
rline _ci_lb _ci_ub _at1 if id==1, lcolor(black) || ///
rspike _ci_lb _ci_ub _at1 if id==0, scheme(plotplain) lcolor(black) yline(0) ylab(,labsize(medlarge)) xlab(,labsize(medlarge)) ///
legend(off) xtitle(Increase in Unemployment, size(medlarge)) ytitle(Effect of having a New Incumbent, size(medlarge))
graph export apd_natexp_hein.eps, replace
restore

*Balance Test (S14)
gen balance=.
replace balance=a07
eststo a: mixed   balance inc_dif if amal==1, vce(cluster muni)
replace balance=bluevotes
eststo b: mixed  balance inc_dif i.muni if amal==1, vce(cluster muni)
replace balance=logvotes
eststo c: mixed   balance inc_dif i.muni if amal==1, vce(cluster muni)
replace balance=turnout
eststo d: mixed  balance inc_dif i.muni if amal==1, vce(cluster muni)

la var balance "%Effect"
esttab a b c d using balancetab.tex, replace keep(inc_dif) star(+ 0.10 * 0.05) b(%9.2fc) ///
mtitles("Unemployment" "Right-wing" "Log(votes)" "Turnout") title("Effect of Different variables on the Probability of Having a New Incumbent.} \footnotesize \label{balancetab") ///
stats(N, label("Observations") fmt(%9.0fc) ) ///
indicate("\hline Municipality  FE=791.muni", labels("$\checkmark$" " ")) ///
label se nonotes addnotes("Standard errors clustered by municipality in parentheses.") nogaps 
drop balance
